package com.fulu.game.bi.service.impl.market;

import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.po.market.NewUserRemainStat;
import com.fulu.game.bi.entity.vo.market.CommonlyQueryVO;
import com.fulu.game.bi.mysql.bi.market.NewRemainStatDao;
import com.fulu.game.bi.redis.service.market.NewUserRemainStatService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class NewUserRemainStatServiceImpl implements NewUserRemainStatService {

    @Autowired
    private NewRemainStatDao newRemainStatDao;

    @Autowired
    private CommonlyStatService commonlyStatService;

    public static final Map<Integer, String> tableNameMap = new HashMap();

    static {
        tableNameMap.put(QueryTypeStatusEnum.DAY.getType(), "t_new_user_remain_stat_day");
        tableNameMap.put(QueryTypeStatusEnum.WEEK.getType(), "t_new_user_remain_stat_week");
        tableNameMap.put(QueryTypeStatusEnum.MONTH.getType(), "t_new_user_remain_stat_month");
    }

    @Override
    public List<NewUserRemainStat> list(CommonlyQueryVO vo) {
        return commonlyStatService.list(vo, tableNameMap, () -> newRemainStatDao.list(vo));
    }

    @Override
    public void export(CommonlyQueryVO vo) {

        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(vo.getQueryType());
        String site = anEnum.getMsg();

        Map<Integer, String> statTypeMap = new HashMap<>();
        statTypeMap.put(1, "注册");
        statTypeMap.put(2, "付费");
        final String statTypeName = statTypeMap.get(vo.getStatType());

        String fileName = "新用户留存" + statTypeName + vo.getStatTimeStart() + "-" + vo.getStatTimeEnd();

        LinkedHashMap<String, Mapper<NewUserRemainStat>> map = new LinkedHashMap<>();
        map.put("日期", v -> v.getBeginStatDay() + "-" + v.getEndStatDay());
        map.put("渠道分组", v -> v.getSourceGroupName());
        map.put("渠道", v -> v.getSourceName());
        map.put("标签分组", v -> v.getLabelGroupName());
        map.put("标签", v -> v.getLabelName());
        map.put("系统", v -> v.getRegisterTypeName());
        map.put("当日新用户数", v -> v.getNewUserNum() + "");

        switch (anEnum) {
            case DAY:

                map.put("1" + site + "内留存人数", v -> v.getAllNum1() + "");
                map.put("2" + site + "内留存人数", v -> v.getAllNum2() + "");
                map.put("3" + site + "内留存人数", v -> v.getAllNum3() + "");
                map.put("4" + site + "内留存人数", v -> v.getAllNum4() + "");
                map.put("5" + site + "内留存人数", v -> v.getAllNum5() + "");
                map.put("6" + site + "内留存人数", v -> v.getAllNum6() + "");
                map.put("7" + site + "内留存人数", v -> v.getAllNum7() + "");
                map.put("14" + site + "内留存人数", v -> v.getAllNum14() + "");
                map.put("30" + site + "内留存人数", v -> v.getAllNum30() + "");

                map.put("1" + site + "内留存率", v -> v.getRemainRatio1() + "");
                map.put("2" + site + "内留存率", v -> v.getRemainRatio2() + "");
                map.put("3" + site + "内留存率", v -> v.getRemainRatio3() + "");
                map.put("4" + site + "内留存率", v -> v.getRemainRatio4() + "");
                map.put("5" + site + "内留存率", v -> v.getRemainRatio5() + "");
                map.put("6" + site + "内留存率", v -> v.getRemainRatio6() + "");
                map.put("7" + site + "内留存率", v -> v.getRemainRatio7() + "");
                map.put("14" + site + "内留存率", v -> v.getRemainRatio14() + "");
                map.put("30" + site + "内留存率", v -> v.getRemainRatio30() + "");

                break;
            default:
                map.put("1" + site + "内留存人数", v -> v.getAllNum1() + "");
                map.put("2" + site + "内留存人数", v -> v.getAllNum2() + "");
                map.put("3" + site + "内留存人数", v -> v.getAllNum3() + "");
                map.put("4" + site + "内留存人数", v -> v.getAllNum4() + "");
                map.put("5" + site + "内留存人数", v -> v.getAllNum5() + "");
                map.put("6" + site + "内留存人数", v -> v.getAllNum6() + "");
                map.put("7" + site + "内留存人数", v -> v.getAllNum7() + "");
                map.put("8" + site + "内留存人数", v -> v.getAllNum8() + "");
                map.put("9" + site + "内留存人数", v -> v.getAllNum9() + "");
                map.put("10" + site + "内留存人数", v -> v.getAllNum10() + "");
                map.put("11" + site + "内留存人数", v -> v.getAllNum11() + "");
                map.put("12" + site + "内留存人数", v -> v.getAllNum12() + "");

                map.put("1" + site + "内留存率", v -> v.getRemainRatio1() + "");
                map.put("2" + site + "内留存率", v -> v.getRemainRatio2() + "");
                map.put("3" + site + "内留存率", v -> v.getRemainRatio3() + "");
                map.put("4" + site + "内留存率", v -> v.getRemainRatio4() + "");
                map.put("5" + site + "内留存率", v -> v.getRemainRatio5() + "");
                map.put("6" + site + "内留存率", v -> v.getRemainRatio6() + "");
                map.put("7" + site + "内留存率", v -> v.getRemainRatio7() + "");
                map.put("8" + site + "内留存率", v -> v.getRemainRatio8() + "");
                map.put("9" + site + "内留存率", v -> v.getRemainRatio9() + "");
                map.put("10" + site + "内留存率", v -> v.getRemainRatio10() + "");
                map.put("11" + site + "内留存率", v -> v.getRemainRatio11() + "");
                map.put("12" + site + "内留存率", v -> v.getRemainRatio12() + "");
                break;
        }

        Querier querier = (num) -> {
            List<NewUserRemainStat> list = list(vo);
            PageInfo pageInfo = new PageInfo(list);
            pageInfo.setHasNextPage(false);
            return pageInfo;
        };

        CsvExportUtil.export(100, fileName, map, querier);
    }
}
